﻿//=================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：江怀玉
// 日 期：2011-10-14
// 描 述：页面基类，所有的aspx页面继承自基类
//        页面基类提供常用的方法和常用的用户信息
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/10/11   江怀玉           
//=================================================

using System;
using ZX.DBModule;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;

namespace ZX.Web.UI.Controls
{
    public class MasterDetailPage : EditUIPage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //this.CreateEntitysInstance();
        }

  
       /// 初始化页面
        /// </summary>
        public void InitPage(){
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string id = this.GetStringFromQueryString("id", "");
            BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            BaseEntity entity = bInstance.GetEntityById(id) as BaseEntity;
            System.Web.UI.WebControls.Literal li = (this.Page.FindControl("EditTemplate") as System.Web.UI.WebControls.Literal);
            if (li != null)
            {
                li.Text = CreateInstanceJsObject(instance, entity);
            }


            EditUIConfig ConfigEntity = EditUIConfig.New();
            bInstance.entity = ConfigEntity;
            bInstance.Order = "iOrder";
            string formReForm = this.GetStringFromQueryString("formReForm", "false");

            IList<EditUIConfig> list = bInstance.GetEntitys<EditUIConfig>("EntityName='" + strEntity + "'");
            int? ColIndex = list.Max(p => p.ColumnIndex);
            StringBuilder strHtml = new StringBuilder();
            if (!string.Equals(formReForm, "false"))
            {
                strHtml.Append(CreateEditReForm(list));
            }
            else
            {
                strHtml.Append("<Table id=\"EditForm\"><tr>");
                for (int i = 0; i < ColIndex + 1; i++)
                {
                    strHtml.Append("<td valign='top' id=\"Col" + i.ToString() + "\"> ");

                    if (entity == null)
                    {
                        strHtml.Append(CreateEditForm(list.Where(p => p.ColumnIndex == i).ToList()));
                    }
                    else
                    {
                        strHtml.Append(CreateEditForm(entity, list.Where(p => p.ColumnIndex == i).ToList()));
                    }
                    strHtml.Append("</td>");
                }
                strHtml.Append("</tr></table>");
            }


            li = (this.Page.FindControl("FormTemplate") as System.Web.UI.WebControls.Literal);
            if (li != null)
            {
                li.Text = strHtml.ToString();
            }
            li = (this.Page.FindControl("jSave") as System.Web.UI.WebControls.Literal);
            if(li!=null)
            {
            li.Text = CreateScript(list);
            }
            li = (this.Page.FindControl("jInitControl") as System.Web.UI.WebControls.Literal);
            if (li != null)
            {
                li.Text = InitControl(list);
            }
            li=(this.Page.FindControl("MasterID") as System.Web.UI.WebControls.Literal);
            if (li != null)
            {
                li.Text = SetMasterID();
            }
            
        }

        /// <summary>
        /// 新实例对象
        /// </summary>
        public string BlankInstance
        {
            get
            {
               object o =ZX.Common.Reflection.CreateEntitysInstance(SubEntityName);
               if (o == null)
               {
                   return "";
               }
               return ZX.Common.Globals.ObjectToJSON(o);
            }
        }


        public string MasterEntityID
        {
            get
            {
                return this.EntityId;
            }
        }

        public string SubEntityID
        {
            get
            {
                return GetStringFromQueryString("subEntityID", "");
            }
        }

        public string SubEntityName
        {
            get
            {
                return GetStringFromQueryString("subEntityName", "");
            }
        }

        /// <summary>
        /// 定义数据字段
        /// </summary>
        public string DataFields
        {
            get
            {
                return GetDataFields();
            }
        }

        /// <summary>
        /// 定义数据列
        /// </summary>
        public string DataColums
        {
            get
            {
                return GetColums();
            }
        }

        
        IList<DataListTemplate> list;
        /// <summary>
        /// 获取字段列
        /// </summary>
        /// <returns></returns>
        private string GetDataFields()
        {
            if (list == null)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                list = bll.GetEntityListByCondition<DataListTemplate>(" EntityName='" + SubEntityName + "'");
            }
            string _strHtml = "key:'{0}',label:'{1}',width:{2},{3}";
            if (list == null)
            {
                return ("{" + string.Format(_strHtml, "Not Data", "Not Data", 400) + "},");
            }
            StringBuilder _sbHtml = new StringBuilder("");
            var _list = list.Where(p => p.IsVisiable == "Y").OrderBy(p => p.iOrder).ToList();
            int i = 0;
            foreach (DataListTemplate _template in _list)
            {
                i = i + 1;
                //if (i == _list.Count())
                //{
                //    _sbHtml.Append("{" + string.Format(_strHtml, _template.Field, _template.THCaption,
                //        "100%"
                //        , string.IsNullOrEmpty(_template.DataFormatter) ? "sortable:true" : (_template.DataFormatter + ",sortable:true")) + "},");
                //}
                //else
                {
                    _sbHtml.Append("{" + string.Format(_strHtml, _template.Field, _template.THCaption,
                     (_template.Width > 0 ? _template.Width : 200)
                     , string.IsNullOrEmpty(_template.DataFormatter) ? "sortable:true,resizeable:true" : "formatter:" + (_template.DataFormatter + ",sortable:true,resizeable:true")) + "},");
                }
                if (i == 1)
                {
                    _sbHtml.Append("{" + string.Format(_strHtml, "iSun_Selected", "", 10, "formatter:\"checkbox\"") + "},");
                }


            }
            _sbHtml.Remove(_sbHtml.Length - 1, 1);
            return (_sbHtml.ToString());
        }

        private string GetDetailDataFields()
        {
            if (list == null)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                list = bll.GetEntityListByCondition<DataListTemplate>(" EntityName='" + SubEntityName + "'");
            }
            string _strHtml = "key:'{0}',label:'{1}',width:{2},{3}";
            if (list == null)
            {
                return ("{" + string.Format(_strHtml, "Not Data", "Not Data", 400) + "},");
            }
            StringBuilder _sbHtml = new StringBuilder("");
            var _list = list.Where(p => p.IsVisiable == "Y").OrderBy(p => p.iOrder).ToList();
            int i = 0;
            foreach (DataListTemplate _template in _list)
            {
                i = i + 1;
                //if (i == _list.Count())
                //{
                //    _sbHtml.Append("{" + string.Format(_strHtml, _template.Field, _template.THCaption,
                //        "100%"
                //        , string.IsNullOrEmpty(_template.DataFormatter) ? "sortable:true" : (_template.DataFormatter + ",sortable:true")) + "},");
                //}
                //else
                {
                    _sbHtml.Append("{" + string.Format(_strHtml, _template.Field, _template.THCaption,
                     (_template.Width > 0 ? _template.Width : 200)
                     , string.IsNullOrEmpty(_template.DataFormatter) ? "sortable:true,resizeable:true,editor: new YAHOO.widget.TextareaCellEditor()" : "formatter:" + (_template.DataFormatter + ",sortable:true,resizeable:true,editor: new YAHOO.widget.TextareaCellEditor()")) + "},");
                }
                if (i == 1)
                {
                    _sbHtml.Append("{" + string.Format(_strHtml, "iSun_Selected", "", 10, "formatter:\"checkbox\"") + "},");
                }


            }
            _sbHtml.Remove(_sbHtml.Length - 1, 1);
            return (_sbHtml.ToString());
        }

        /// <summary>
        /// 获得表头
        /// </summary>
        /// <returns></returns>
        public string GetColums()
        {
            if (list == null)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                list = bll.GetEntityListByCondition<DataListTemplate>("EntityName='" + EntityName + "'");
            }
            if (list == null)
            {
                return "";
            }

            StringBuilder _sbHtml = new StringBuilder("");
            var _list = list.OrderBy(p => p.iOrder);
            int i = 0;
            foreach (DataListTemplate _template in _list)
            {
                _sbHtml.Append("'" + _template.Field + "',");
                if (i == 0)
                {
                    _sbHtml.Append("'iSun_Selected',");
                    i = 1;
                }
            }
            _sbHtml.Remove(_sbHtml.Length - 1, 1);
            return (_sbHtml.ToString());
        }


        /// <summary>
        /// 获得表头
        /// </summary>
        /// <returns></returns>
        public string GetColumsCss()
        {
            if (list == null)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                list = bll.GetEntityListByCondition<DataListTemplate>(" EntityName='" + EntityName + "'");
            }
            string _strHtml = ".yui-skin-sam .yui-dt-col-{0}{width:{1}}\n";
            if (list == null)
            {
                return (string.Format(_strHtml, "Not Data", "Not Data", 400));
            }
            StringBuilder _sbHtml = new StringBuilder("");
            var _list = list.OrderBy(p => p.iOrder);
            int i = 0;
            foreach (DataListTemplate _template in _list)
            {
                i = i + 1;
                if (i == _list.Count())
                {
                    _sbHtml.Append(_strHtml.Replace("{0}", _template.Field).Replace("{1}", "100%"));
                }
                else
                {
                    _sbHtml.Append(_strHtml.Replace("{0}", _template.Field).Replace("{1}", _template.Width < 200 ? "200px" : _template.Width.ToString() + "px"));
                }
            }
            _sbHtml.Remove(_sbHtml.Length - 1, 1);
            return (_sbHtml.ToString());
        }
        
    }
}
